{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "93869c17",
   "metadata": {},
   "source": [
    "# ExtendedKalmanFilter\n",
    "\n",
    "## Overview\n",
    "\n",
    "The `ExtendedKalmanFilter` class in GTSAM is an implementation of the [Extended Kalman Filter (EKF)](https://en.wikipedia.org/wiki/Extended_Kalman_filter), which is a powerful tool for estimating the state of a nonlinear dynamic system.\n",
    "\n",
    "See also [this notebook](../../../python/gtsam/examples/easyPoint2KalmanFilter.ipynb) for the python version of the C++ example below."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "161c36eb",
   "metadata": {},
   "source": [
    "## Using the ExtendedKalmanFilter Class\n",
    "\n",
    "The `ExtendedKalmanFilter` class in GTSAM provides a flexible way to implement Kalman filtering using factor graphs. Here's a step-by-step guide based on the example provided in [easyPoint2KalmanFilter.cpp](https://github.com/borglab/gtsam/blob/develop/examples/easyPoint2KalmanFilter.cpp):\n",
    "\n",
    "### Steps to Use the ExtendedKalmanFilter\n",
    "\n",
    "1. **Initialize the Filter**:\n",
    "   - Define the initial state (e.g., position) and its covariance.\n",
    "   - Create a key for the initial state.\n",
    "   - Instantiate the `ExtendedKalmanFilter` object with the initial state and covariance.\n",
    "\n",
    "   ```cpp\n",
    "   Point2 x_initial(0.0, 0.0);\n",
    "   SharedDiagonal P_initial = noiseModel::Diagonal::Sigmas(Vector2(0.1, 0.1));\n",
    "   Symbol x0('x', 0);\n",
    "   ExtendedKalmanFilter<Point2> ekf(x0, x_initial, P_initial);\n",
    "   ```\n",
    "\n",
    "2. Predict the Next State:\n",
    "\n",
    "   - Define the motion model using a BetweenFactor.\n",
    "   - Predict the next state using the predict method.\n",
    "   ```cpp\n",
    "   Symbol x1('x', 1);\n",
    "   Point2 difference(1, 0);\n",
    "   SharedDiagonal Q = noiseModel::Diagonal::Sigmas(Vector2(0.1, 0.1), true);\n",
    "   BetweenFactor<Point2> factor1(x0, x1, difference, Q);\n",
    "   Point2 x1_predict = ekf.predict(factor1);\n",
    "   ```\n",
    "\n",
    "3. Update the State with Measurements:\n",
    "   - Define the measurement model using a PriorFactor.\n",
    "   - Update the state using the update method.\n",
    "   ```cpp\n",
    "   Point2 z1(1.0, 0.0);\n",
    "   SharedDiagonal R = noiseModel::Diagonal::Sigmas(Vector2(0.25, 0.25), true);\n",
    "   PriorFactor<Point2> factor2(x1, z1, R);\n",
    "   Point2 x1_update = ekf.update(factor2);\n",
    "   ```\n",
    "4. Repeat for Subsequent Time Steps:\n",
    "\n",
    "   - Repeat the prediction and update steps for subsequent states and measurements.\n",
    "\n",
    "## Example Use Case\n",
    "This example demonstrates tracking a moving 2D point using a simple linear motion model and position measurements. The ExtendedKalmanFilter class allows for flexible modeling of both the motion and measurement processes using GTSAM's factor graph framework.\n",
    "\n",
    "For the full implementation, see the [easyPoint2KalmanFilter.cpp](https://github.com/borglab/gtsam/blob/develop/examples/easyPoint2KalmanFilter.cpp) file.\n"
   ]
  }
 ],
 "metadata": {
  "language_info": {
   "name": "python"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
